home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Button.java < prev    next >
Text File  |  1998-09-22  |  10KB  |  305 lines

  1. /*
  2.  * @(#)Button.java    1.43 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. import java.awt.peer.ButtonPeer;
  18. import java.awt.event.*;
  19. import java.io.ObjectOutputStream;
  20. import java.io.ObjectInputStream;
  21. import java.io.IOException;
  22.  
  23.  
  24. /**
  25.  * This class creates a labeled button. The application can cause 
  26.  * some action to happen when the button is pushed. This image 
  27.  * depicts three views of a "<code>Quit</code>" button as it appears
  28.  * under the Solaris operating system: 
  29.  * <p> 
  30.  * <img src="images-awt/Button-1.gif"
  31.  * ALIGN=center HSPACE=10 VSPACE=7>  
  32.  * <p>
  33.  * The first view shows the button as it appears normally. 
  34.  * The second view shows the button 
  35.  * when it has input focus. Its outline is darkened to let the 
  36.  * user know that it is an active object. The third view shows the 
  37.  * button when the user clicks the mouse over the button, and thus 
  38.  * requests that an action be performed.
  39.  * <p>
  40.  * The gesture of clicking on a button with the mouse
  41.  * is associated with one instance of <code>ActionEvent</code>, 
  42.  * which is sent out when the mouse is both pressed and released 
  43.  * over the button. If an application is interested in knowing
  44.  * when the button has been pressed but not released, as a separate 
  45.  * gesture, it can specialize <code>processMouseEvent</code>, 
  46.  * or it can register itself as a listener for mouse events by
  47.  * calling <code>addMouseListener</code>. Both of these methods are
  48.  * defined by <code>Component</code>, the abstract superclass of
  49.  * all components.
  50.  * <p>
  51.  * When a button is pressed and released, AWT sends an instance  
  52.  * of <code>ActionEvent</code> to the button, by calling 
  53.  * <code>processEvent</code> on the button. The button's 
  54.  * <code>processEvent</code> method receives all events
  55.  * for the button; it passes an action event along by
  56.  * calling its own <code>processActionEvent</code> method.
  57.  * The latter method passes the action event on to any action
  58.  * listeners that have registered an interest in action
  59.  * events generated by this button.
  60.  * <p>
  61.  * If an application wants to perform some action based on  
  62.  * a button being pressed and released, it should implement 
  63.  * <code>ActionListener</code> and register the new listener 
  64.  * to receive events from this button, by calling the button's
  65.  * <code>addActionListener</code> method. The application can
  66.  * make use of the button's action command as a messaging protocol.
  67.  *
  68.  * @version     1.39 06/12/97
  69.  * @author     Sami Shaio
  70.  * @see         java.awt.event.ActionEvent
  71.  * @see         java.awt.event.ActionListener
  72.  * @see         java.awt.Component#processMouseEvent
  73.  * @see         java.awt.Component#addMouseListener
  74.  * @since       JDK1.0
  75.  */
  76. public class Button extends Component {
  77.  
  78.     String label;
  79.     String actionCommand;
  80.  
  81.     transient ActionListener actionListener;
  82.     
  83.     private static final String base = "button";
  84.     private static int nameCounter = 0;
  85.     
  86.     /*
  87.      * JDK 1.1 serialVersionUID 
  88.      */
  89.     private static final long serialVersionUID = -8774683716313001058L;
  90.  
  91.     /**
  92.      * Constructs a Button with no label.
  93.      * @since JDK1.0
  94.      */
  95.     public Button() {
  96.     this("");
  97.     }
  98.  
  99.     /**
  100.      * Constructs a Button with the specified label.
  101.      * @param label A string label for the button.
  102.      * @since JDK1.0
  103.      */
  104.     public Button(String label) {
  105.     this.label = label;
  106.     }
  107.  
  108.     /**
  109.      * Construct a name for this component.  Called by getName() when the
  110.      * name is null.
  111.      */
  112.     String constructComponentName() {
  113.     return base + nameCounter++;
  114.     }
  115.  
  116.     /**
  117.      * Creates the peer of the button.  The button's peer allows the
  118.      * application to change the look of the button without changing 
  119.      * its functionality.
  120.      * @see     java.awt.Toolkit#createButton(java.awt.Button)
  121.      * @see     java.awt.Component#getToolkit()
  122.      * @since   JDK1.0
  123.      */
  124.     public void addNotify() {
  125.         synchronized(getTreeLock()) {
  126.         if (peer == null)
  127.             peer = getToolkit().createButton(this);
  128.         super.addNotify();
  129.         }
  130.     }
  131.  
  132.     /**
  133.      * Gets the label of this button.
  134.      * @return    the button's label, or <code>null</code> 
  135.      *                if the button has no label.
  136.      * @see       java.awt.Button#setLabel
  137.      * @since     JDK1.0
  138.      */
  139.     public String getLabel() {
  140.     return label;
  141.     }
  142.  
  143.     /**
  144.      * Sets the button's label to be the specified string.
  145.      * @param     label   the new label, or <code>null</code> 
  146.      *                if the button has no label.
  147.      * @see       java.awt.Button#getLabel
  148.      * @since     JDK1.0
  149.      */
  150.     public synchronized void setLabel(String label) {
  151.     this.label = label;
  152.     ButtonPeer peer = (ButtonPeer)this.peer;
  153.     if (peer != null) {
  154.         peer.setLabel(label);
  155.     }
  156.     }
  157.  
  158.     /**
  159.      * Sets the command name for the action event fired 
  160.      * by this button. By default this action command is 
  161.      * set to match the label of the button.
  162.      * @param     command  A string used to set the button's 
  163.      *                  action command.
  164.      * @see       java.awt.event.ActionEvent
  165.      * @since     JDK1.1
  166.      */
  167.     public void setActionCommand(String command) {
  168.         actionCommand = command;
  169.     }
  170.  
  171.     /**
  172.      * Returns the command name of the action event fired by this button.
  173.      */
  174.     public String getActionCommand() {
  175.         return (actionCommand == null? label : actionCommand);
  176.     }
  177.  
  178.     /**
  179.      * Adds the specified action listener to receive action events from
  180.      * this button. Action events occur when a user presses or releases
  181.      * the mouse over this button.
  182.      * @param         l the action listener.
  183.      * @see           java.awt.event.ActionListener
  184.      * @see           java.awt.Button#removeActionListener
  185.      * @since         JDK1.1
  186.      */ 
  187.     public synchronized void addActionListener(ActionListener l) {
  188.     actionListener = AWTEventMulticaster.add(actionListener, l);
  189.         newEventsOnly = true;    
  190.     }
  191.  
  192.     /**
  193.      * Removes the specified action listener so that it no longer 
  194.      * receives action events from this button. Action events occur  
  195.      * when a user presses or releases the mouse over this button.
  196.      * @param         l     the action listener.
  197.      * @see           java.awt.event.ActionListener
  198.      * @see           java.awt.Button#addActionListener
  199.      * @since         JDK1.1
  200.      */ 
  201.     public synchronized void removeActionListener(ActionListener l) {
  202.     actionListener = AWTEventMulticaster.remove(actionListener, l);
  203.     }
  204.  
  205.     // REMIND: remove when filtering is done at lower level
  206.     boolean eventEnabled(AWTEvent e) {
  207.         if (e.id == ActionEvent.ACTION_PERFORMED) {
  208.             if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
  209.                 actionListener != null) {
  210.                 return true;
  211.             }
  212.             return false;
  213.         }
  214.         return super.eventEnabled(e);
  215.     }          
  216.  
  217.     /**
  218.      * Processes events on this button. If an event is 
  219.      * an instance of <code>ActionEvent</code>, this method invokes  
  220.      * the <code>processActionEvent</code> method. Otherwise,
  221.      * it invokes <code>processEvent</code> on the superclass.
  222.      * @param        e the event.
  223.      * @see          java.awt.event.ActionEvent
  224.      * @see          java.awt.Button#processActionEvent
  225.      * @since        JDK1.1
  226.      */
  227.     protected void processEvent(AWTEvent e) {
  228.         if (e instanceof ActionEvent) {
  229.             processActionEvent((ActionEvent)e);     
  230.             return;
  231.         }
  232.     super.processEvent(e);
  233.     }
  234.  
  235.     /** 
  236.      * Processes action events occurring on this button 
  237.      * by dispatching them to any registered 
  238.      * <code>ActionListener</code> objects.
  239.      * <p>
  240.      * This method is not called unless action events are 
  241.      * enabled for this button. Action events are enabled 
  242.      * when one of the following occurs:
  243.      * <p><ul>
  244.      * <li>An <code>ActionListener</code> object is registered 
  245.      * via <code>addActionListener</code>.
  246.      * <li>Action events are enabled via <code>enableEvents</code>.
  247.      * </ul>
  248.      * @param       e the action event.
  249.      * @see         java.awt.event.ActionListener
  250.      * @see         java.awt.Button#addActionListener
  251.      * @see         java.awt.Component#enableEvents
  252.      * @since       JDK1.1
  253.      */  
  254.     protected void processActionEvent(ActionEvent e) {
  255.         if (actionListener != null) {
  256.             actionListener.actionPerformed(e);
  257.         }
  258.     }
  259.  
  260.     /**
  261.      * Returns the parameter string representing the state of this 
  262.      * button. This string is useful for debugging. 
  263.      * @return     the parameter string of this button.
  264.      * @since      JDK1.0
  265.      */
  266.     protected String paramString() {
  267.     return super.paramString() + ",label=" + label;
  268.     }
  269.  
  270.  
  271.     /* Serialization support. 
  272.      */
  273.  
  274.     private int buttonSerializedDataVersion = 1;
  275.  
  276.  
  277.     private void writeObject(ObjectOutputStream s)
  278.       throws IOException 
  279.     {
  280.       s.defaultWriteObject();
  281.  
  282.       AWTEventMulticaster.save(s, actionListenerK, actionListener);
  283.       s.writeObject(null);
  284.     }
  285.  
  286.  
  287.     private void readObject(ObjectInputStream s)
  288.       throws ClassNotFoundException, IOException 
  289.     {
  290.       s.defaultReadObject();
  291.  
  292.       Object keyOrNull;
  293.       while(null != (keyOrNull = s.readObject())) {
  294.     String key = ((String)keyOrNull).intern();
  295.  
  296.     if (actionListenerK == key) 
  297.       addActionListener((ActionListener)(s.readObject()));
  298.  
  299.     else // skip value for unrecognized key
  300.       s.readObject();
  301.       }
  302.     }
  303.   
  304. }
  305.